package cn.iocoder.yudao.module.blog.controller.app.improve;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.blog.controller.admin.improve.vo.BlogImprovePageReqVO;
import cn.iocoder.yudao.module.blog.controller.admin.improve.vo.BlogImproveRespVO;
import cn.iocoder.yudao.module.blog.convert.improve.BlogImproveConvert;
import cn.iocoder.yudao.module.blog.dal.dataobject.improve.BlogImproveDO;
import cn.iocoder.yudao.module.blog.dal.dataobject.improvechapter.BlogImproveChapterDO;
import cn.iocoder.yudao.module.blog.dal.dataobject.improvemanager.BlogImproveManagerDO;
import cn.iocoder.yudao.module.blog.dal.dataobject.improvesection.BlogImproveSectionDO;
import cn.iocoder.yudao.module.blog.service.improve.BlogImproveService;
import cn.iocoder.yudao.module.blog.service.improvechapter.BlogImproveChapterService;
import cn.iocoder.yudao.module.blog.service.improvemanager.BlogImproveManagerService;
import cn.iocoder.yudao.module.blog.service.improvemanager.BlogImproveManagerServiceImpl;
import cn.iocoder.yudao.module.blog.service.improvesection.BlogImproveSectionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.ArrayList;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "用户 APP - 课程")
@RestController
@RequestMapping("/blog/improve")
@Validated
@Slf4j
public class AppBlogImproveController {

    @Resource
    private BlogImproveService improveService;

    @Resource
    private BlogImproveSectionService improveSectionService;

    @Resource
    private BlogImproveManagerService improveManagerService;

    @Resource
    private BlogImproveChapterService improveChapterService;

    @GetMapping("/page")
    @Operation(summary = "获得课程分页")
    public CommonResult<PageResult<BlogImproveRespVO>> getImprovePage(@Valid BlogImprovePageReqVO pageVO) {
        PageResult<BlogImproveDO> pageResult = improveService.getImprovePage(pageVO);
        return success(BlogImproveConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/get")
    @Operation(summary = "获得课程")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<BlogImproveRespVO> getImprove(@RequestParam("id") Long id) {
        BlogImproveDO improve = improveService.getImprove(id);

        List<BlogImproveChapterDO> chapterDOList = improveChapterService.getImproveChapterByImproveId(id);

        List<BlogImproveSectionDO> sectionList = new ArrayList<>();
        if(improve != null){
             sectionList = improveSectionService.getImproveSectionByImproveId(improve.getId());
        }

        return success(BlogImproveConvert.INSTANCE.convert(improve, chapterDOList, sectionList));
    }

    @GetMapping("/member-improve")
    @Operation(summary = "获得会员的全部课程")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<List<BlogImproveManagerDO>> getMemberImprove(@RequestParam("id") Long memberId){
        List<BlogImproveManagerDO> list = improveManagerService.getMemberImproveList(memberId);
        return success(list);
    }
}
